package com.yixun.qfbao.api.service.impl;

import com.yixun.common.page.Page;
import com.yixun.common.page.PageUtil;
import com.yixun.qfbao.api.service.BaseCodeValueService;
import com.yixun.qfbao.apidto.*;
import com.yixun.qfbao.mapper.BaseCodeValueMapper;
import com.yixun.qfbao.model.BaseCodeValue;
import com.yixun.qfbao.model.dto.BaseCodeValueDto;
import com.yixun.qfbao.model.enums.DelFlagEnum;
import com.yixun.qfbao.xf.model.enums.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author guifang.weng
 * @create 2017-11-24 11:50
 **/
@Service
public class BaseCodeValueServiceImpl implements BaseCodeValueService {

    @Autowired
    private BaseCodeValueMapper baseCodeValueMapper;

    @Override
    public GetBaseCodeValueListResDto getBaseCodeValueList(GetBaseCodeValueListReqDto getBaseCodeValueListReqDto) throws Exception {
        GetBaseCodeValueListResDto resDto = new GetBaseCodeValueListResDto();
        BaseCodeValueDto dto = new BaseCodeValueDto();
        dto.setMerchantId(getBaseCodeValueListReqDto.getMerchantId());
        if (getBaseCodeValueListReqDto.getTypeId() != null){
            dto.setTypeId(Long.valueOf(getBaseCodeValueListReqDto.getTypeId()));
        }
        List<BaseCodeValue> baseCodeValueList = baseCodeValueMapper.selectListByDto(dto);
        resDto.setResult(ResultEnum.SUCCESS.getNo());
        resDto.setBaseCodeValueList(baseCodeValueList);
        return resDto;
    }

    @Override
    public GetBaseCodeValuePageResDto getBaseCodeValuePage(GetBaseCodeValuePageReqDto getBaseCodeValuePageReqDto) throws Exception {
        GetBaseCodeValuePageResDto resDto = new GetBaseCodeValuePageResDto();
        BaseCodeValueDto entity = new BaseCodeValueDto();
        entity.setCodeName(getBaseCodeValuePageReqDto.getCodeName());
        if (getBaseCodeValuePageReqDto.getTypeId() != null) {
            entity.setTypeId(Long.valueOf(getBaseCodeValuePageReqDto.getTypeId()));
        }
        entity.setMerchantId(getBaseCodeValuePageReqDto.getMerchantId());
        entity.setOrders("create_time desc");
        Integer pageNo = getBaseCodeValuePageReqDto.getPageNo();
        Integer pageSize = getBaseCodeValuePageReqDto.getPageSize();
        Integer total = baseCodeValueMapper.myCount(entity);
        List<BaseCodeValue> baseCodeValueList = baseCodeValueMapper.myList(entity, PageUtil.begin(pageNo, pageSize), pageSize);
        Page<BaseCodeValue> pageModel = new Page<BaseCodeValue>(total,baseCodeValueList);
        pageModel.setPageInfo(pageModel.getTotal(), PageUtil.begin(pageNo, pageSize), pageSize * pageNo);
        resDto.setResult(ResultEnum.SUCCESS.getNo());
        resDto.setBaseCodeValuePage(pageModel);
        return resDto;
    }

    @Override
    public AddBaseCodeValueResDto addBaseCodeValue(AddBaseCodeValueReqDto addBaseCodeValueReqDto) throws Exception {
        AddBaseCodeValueResDto resDto = new AddBaseCodeValueResDto();
        BaseCodeValue entity = new BaseCodeValue();
        entity.setMerchantId(addBaseCodeValueReqDto.getMerchantId());
        entity.setTypeId(Long.parseLong(addBaseCodeValueReqDto.getTypeId()));
        entity.setCodeName(addBaseCodeValueReqDto.getCodeName());
        entity.setRemark(addBaseCodeValueReqDto.getRemark());
        entity.setCreateUserId(Long.valueOf(addBaseCodeValueReqDto.getUserId()));
        entity.setCreateTime(new Date());
        entity.setDelFlag(DelFlagEnum.UNDEL.getNo());
        if(baseCodeValueMapper.add(entity) <= 0){
            resDto.setResult(ResultEnum.FAIL.getNo());
            resDto.setResultMsg("新增类型值失败");
        }else {
            resDto.setResult(ResultEnum.SUCCESS.getNo());
            resDto.setResultMsg("新增类型值成功");
        }
        return resDto;
    }

    @Override
    public EditBaseCodeValueResDto editBaseCodeValue(EditBaseCodeValueReqDto editBaseCodeValueReqDto) throws Exception {
        EditBaseCodeValueResDto resDto = new EditBaseCodeValueResDto();
        BaseCodeValue entity = new BaseCodeValue();
        entity.setId(editBaseCodeValueReqDto.getId());
        entity.setTypeId(Long.parseLong(editBaseCodeValueReqDto.getTypeId()));
        entity.setCodeName(editBaseCodeValueReqDto.getCodeName());
        entity.setRemark(editBaseCodeValueReqDto.getRemark());
        entity.setUpdateUserId(Long.valueOf(editBaseCodeValueReqDto.getUserId()));
        entity.setUpdateTime(new Date());
        if (baseCodeValueMapper.edit(entity) <= 0) {
            resDto.setResult(ResultEnum.FAIL.getNo());
            resDto.setResultMsg("编辑类型值失败");
        }else {
            resDto.setResult(ResultEnum.SUCCESS.getNo());
            resDto.setResultMsg("编辑类型值成功");
        }
        return resDto;
    }

    @Override
    public DelBaseCodeValueResDto delBaseCodeValue(DelBaseCodeValueReqDto delBaseCodeValueReqDto) throws Exception {
        DelBaseCodeValueResDto resDto = new DelBaseCodeValueResDto();
        BaseCodeValueDto baseCodeValue = new BaseCodeValueDto();
        baseCodeValue.setId(delBaseCodeValueReqDto.getId());
        if(baseCodeValueMapper.del(baseCodeValue) <= 0){
            resDto.setResult(ResultEnum.FAIL.getNo());
            resDto.setResultMsg("删除类型值失败");
        }else {
            resDto.setResult(ResultEnum.SUCCESS.getNo());
            resDto.setResultMsg("删除类型值成功");
        }
        return resDto;
    }

    @Override
    public int count(BaseCodeValueDto bcvDto) {
        return baseCodeValueMapper.count(bcvDto);
    }
}
